package nl.maarten.docenthelp.manager;

import java.util.ArrayList;

import nl.maarten.docenthelp.data.Database;
import nl.maarten.docenthelp.entity.Problem;

public class ProblemManager
{
	private static ProblemManager instanceManager;
	
	private ArrayList<Problem> problems;
	
	private Database db;
	
	public static ProblemManager getInstance()
	{
		if(instanceManager == null)
		{
			instanceManager = new ProblemManager();
		}
		
		return instanceManager;
	}
	
	private ProblemManager()
	{
		db = Database.getInstance();
		
		problems = new ArrayList<Problem>();
		
		refreshProblems();
	}
	
	public void refreshProblems()
	{
		for( Problem problem : problems )
		{
			if( problem.needsUpdate() )
			{
				db.updateProblem( problem );
			}
		}
		problems.clear();
		
		problems = db.getProblems();
	}
	
	public ArrayList<Problem> getProblems()
	{
		return problems;
	}
	
	public void problemCorrected(int id)
	{
		for(int i = 0; i < problems.size(); ++i)
		{
			if(problems.get( i ).getID() == id)
			{
				problems.get( i ).setCorrected( true );
				problems.get( i ).save();
			}
		}
	}

	public ArrayList<Problem> getActiveProblems()
	{
		ArrayList<Problem> ret = new ArrayList<Problem>();
		for (int i = 0; i < problems.size(); ++i)
		{
			if (!problems.get(i).isCorrected())
				ret.add(problems.get(i));
		}
		
		return ret;
	}
}
